var step_search_obj={
	step_search:$('#step-search'),
	step1:function(){
		this.step_search.show();
	},
	step2:function(){
		this.step_search.hide();
		
	},
	step3:function(){
		this.step_search.hide();
	},
};

var step1_obj={
	step_1:$('#step-1'),
	step1_area:$('#step-1').find(".area"),
	step1_area_li:$('#step-1').find(".area>li"),
	step1:function(){
		this.step_1.show();
		this.bind_event();
		var t_left=this.step1_area.css("margin-left");
		var pyl="";
		this.step1_area_li.removeClass("hidden").addClass("visible");
		this.step1_area_li.removeClass("active");
		if(parseInt(t_left)>0){
			pyl="-="+Math.abs(parseInt(t_left));
		}else{
			pyl="+="+Math.abs(parseInt(t_left));
		}
		this.step1_area.animate({ "margin-left": pyl+"px" }, "slow");
		
	},
	step2:function(p_obj){
		this.step_1.show();
		this.step1_area_li.unbind("click");
		var select_obj=p_obj;
		var select_obj_index= select_obj.index();
		this.step1_area_li.removeClass("active");
		this.step1_area_li.addClass("hidden");
		
		var pyl=Math.abs(135 * (3-select_obj_index));
		if(select_obj_index<3){
			this.step1_area.animate({ "margin-left": "+="+pyl+"px" }, "slow");
		}else if(select_obj_index>3){
			this.step1_area.animate({ "margin-left": "-="+pyl+"px" }, "slow");
		}
		select_obj.removeClass("hidden").addClass("visible");
		select_obj.addClass("active");
		
	},
	step3:function(p_obj){
		
		
		this.step_1.hide();
	},
	bind_event:function(){
		this.step1_area_li.bind("click",function (){//绑定click事件
			step1_obj.step3($(this));
			step3_obj.step3($(this));
			step_search_obj.step3();
		});
	},
	get_select_li:function(){
		return this.step1_area.find(".active");
	}
};

var step2_obj={
	step_2:$('#step-2'),
	step2_title_box:$('#step-2').find("#select_area_title"),
	step2_content_box:$('#step-2').find("#select_area_content"),
	step1:function(){
		this.step_2.hide();
	},
	step2:function(p_obj){
		
		var select_obj=p_obj;
		var select_obj_name=select_obj.find(".city_name").html();
		var select_obj_id=select_obj.attr("data-id");
		this.step2_title_box.html(select_obj_name);
		
		//ajax 通过 select_obj_id 请求获得值 付给 step2_content_box  对象；
		var data=[{"id":1,"name":"白云2"},
		{"id":1,"name":"天河2"},
		{"id":1,"name":"番禺2"},
		{"id":1,"name":"海珠2"}];
		var content=this.format_data(data);
		this.step2_content_box.html(content);
		this.step_2.show();
	},
	step3:function(p_obj){
		
		this.step_2.hide();
	},
	bind_event:function(){
		this.step_2.find(".to_area").bind("click",function(){
			step1_obj.step1();
			step2_obj.step1();
			step_search_obj.step1();
		});
		this.step2_content_box.find("li>a").live("click",function(){
			step1_obj.step3();
			step2_obj.step3();
			step3_obj.step3($(this));
			step_search_obj.step3();
		});
	},
	format_data:function(p_data){
		var count=p_data.length;
		var s_count=3-count%3;
		var e="";
		$.each(p_data,function(h, g) {
			e += '<li><a href="javascript:;" data-id="'+g.id+'" data-needaz="false">'+g.name+'</a></li>';
		});
		if(s_count<3){
			for(var i=0;i<s_count;i++){
				e+='<li style="height:38px;"></li>';
			}
		}
		return e
	}
	
};

var step3_obj={
	step_3:$('#step-3'),
	step3_title_box:$('#step-3').find("#home_nav"),
	step3_content_azgroup:$('#step-3').find(".azgroup"),
	step3_content_restant:$('#step-3').find("#entry_con"),
	step1:function(){
		this.step_3.hide();
	},
	step2:function(p_obj){
		this.step_3.hide();
	},
	step3:function(p_obj){
		var select_obj_1=p_obj;
		var select_obj_1_name=select_obj_1.find(".city_name").html();
		var select_obj_1_id=select_obj_1.attr("data-id");
		
		var title=select_obj_1_name+"<em></em>"+"请选择大厦";
		this.step3_title_box.html(title);
		//====================================
		//alert("aaaaaa");
		$.ajax({
			url:"index.action?action=scriptorium&areaId="+select_obj_1_id,
			dataType:"json", 
			success: function(json){
				var total_data=json;
				var az_str=step3_obj.format_az(total_data.az_data);
				var restant_str=step3_obj.format_reatant(total_data.reatan_data);
				step3_obj.step3_content_azgroup.html(az_str);
			    step3_obj.step3_content_restant.html(restant_str);
				step3_obj.step_3.show();
			}
		});
		
		//=====================================
		
		
		//==================
	},
	bind_event:function(){
		this.step_3.find("#backto_step1").bind("click",function(){
			step1_obj.step1();
			step2_obj.step1();
			step3_obj.step1();
			step_search_obj.step1();
		});
		var t_step3_content_azgroup=this.step3_content_azgroup;
		var t_step3_content_restant=this.step3_content_restant;
		
		t_step3_content_azgroup.find("a").live("click",function(){
			var select_this=$(this);
			t_step3_content_azgroup.find("a").removeClass("active");
			select_this.addClass("active");
			var select_az=select_this.attr("data-val");
			t_step3_content_restant.find("li").show();
			if(select_az!='0'){
				t_step3_content_restant.find("li[data-sn!='"+select_az+"']").hide();
			}
		});
	},
	format_az:function(p_data){
		var e = '<a class="all active" data-val="0" href="javascript:;">全部</a>';
		$.each(p_data,function(h, g) {
			e += '<a class="com" data-val="' + g + '" href="javascript:;">' + g.toLocaleUpperCase() + "</a>"
		});
		return e
	},
	format_reatant:function(p_data) {
			var e = "";
			$.each(p_data,function(h, g) {
						e += '<li data-sn="' + g.sn + '"><a href="'  + "/resList.action?id=" + g.id + '" title="' + g.name + '" class="entry_by_my_entry">' + g.name + '</a><i class="icon-trash"></i></li>'
					
			});
			return e;
	}
	
};

$(document).ready(function(){
	//区域图标注册事件
	step1_obj.bind_event();
	step2_obj.bind_event();
	step3_obj.bind_event();
	$("#searchInput").autocomplete('getRes.action', {
		width: 333,
		max: 40,
		dataType: "json",
		parse: function(data) {
			/**
			data=[{"name":"有色大厦","id":"1"},{"name":"卓越大厦","id":"2"},{"name":"天喜大厦","id":"3"},{"name":"科技大厦","id":"4"},{"name":"华府天地","id":"55"}];
			**/
			
			return $.map(data, function(row) {
				return {
					data: row,
					value: row.id,
					result: row.name
				}
			});
		},
		formatItem: function(item) {
			return  item.name;
		}
	}).result(function(e, item) {
		this.value="";
		//选择后跳转的页面
		location.href="resList.action?id="+item.id
		
		
	});
	
	
});

